<?php

namespace common\models\datas;

use common\components\bases\BaseModel;
use Yii;

/**
 * This is the model class for table "{{%payment}}".
 *
 * @property integer $id
 * @property string $name
 * @property string $code
 * @property string $config
 * @property string $description
 * @property integer $pay_method
 * @property string $pay_fee
 * @property integer $is_cod
 * @property integer $enabled
 * @property integer $sort
 */
class Payment extends BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%payment}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['config', 'description'], 'string'],
            [['pay_method', 'is_cod', 'enabled', 'sort'], 'integer'],
            [['name'], 'string', 'max' => 100],
            ['name', 'required'],
            ['name', 'unique'],
            ['key', 'unique'],
            [['key'], 'string', 'max' => 50],
            [['pay_fee'], 'string', 'max' => 10]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自增ID'),
            'name' => Yii::t('common', '支付方式名称'),
            'key' => Yii::t('common', '支付KEY'),
            'config' => Yii::t('common', '支付方式的配置信息，包括商户号和密钥及其他'),
            'description' => Yii::t('common', '描述'),
            'pay_method' => Yii::t('common', '支付手续费'),
            'pay_fee' => Yii::t('common', '支付手续费'),
            'is_cod' => Yii::t('common', '货到付款？'),
            'enabled' => Yii::t('common', '是否启用'),
            'sort' => Yii::t('common', '排序'),
        ];
    }

    public function fields(){
        $fields = parent::fields();
        $fields['pay_method'] = function(){
                 return $this->pay_method == 0 ?'按比例': '固定';
            };
        $fields['pay_fee'] = function(){
            return $this->pay_method == 0 ? $this->pay_fee.'%': $this->pay_fee.'元';
            };
        return $fields;
        }
    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

}
